﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WilsonWebsite
{
    public partial class Default : System.Web.UI.Page
    {
        private string strCon = Global.strCon;
        private int recommNum = Global.recommNum;
        private int commctrlNum = 0;
        private int hotNewsNum = Global.hotNewsNum;
        private int siteRowNum = Global.siteRowNum;
        protected string imageWeb = Global.imageWeb;
        private int countRow = 0;
        private int countPage = 0;

        protected Dictionary<string, string> dic = new Dictionary<string, string>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindRecommendList(strCon, "1=1");
                BindCommCtrlItem();
                BindHotNews(strCon, 1, hotNewsNum, " where 1=1");
                BindOther();
                ConfigInfo(Global.strCon);
            }
        }

        #region RecommendPro
        private void BindRecommendList(string strCon, string whereStr) 
        {
            using (DataTable dtfl = Bll.RecommendPro.MULTIPLE(strCon, whereStr)) 
            {
                if (dtfl != null && dtfl.Rows.Count > 0)
                {
                    var query = from q in dtfl.AsEnumerable()
                                where Convert.ToInt32(q["RowId"]) <= recommNum
                                select q;
                    commctrlNum = query.Count<DataRow>();
                    this.repCommList.DataSource = query.CopyToDataTable<DataRow>().DefaultView;
                    this.repCommList.DataBind();
                }
            } 
        }

        private DataTable CreateCommCtrlTable() 
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(Int32));
            dt.Columns.Add("Item", typeof(string));
            return dt;
        }

        private void BindCommCtrlItem() 
        {
            DataTable dtcc = CreateCommCtrlTable();
            for (int i = 0; i < commctrlNum; i++) 
            {
                DataRow newRow = dtcc.NewRow();
                newRow["Id"] = i + 1;
                newRow["Item"] = "<a href=\"javascript:void(0);\" class=\"CommCtrlItem\"><span></span></a>";
                dtcc.Rows.Add(newRow);
            }
            if (dtcc != null && dtcc.Rows.Count > 0)
            {
                this.repCommCtrl.DataSource = dtcc.DefaultView;
                this.repCommCtrl.DataBind();
                dtcc.Dispose();
            }
        }
        #endregion

        #region HotNews
        private void BindHotNews(string strCon,int pageIndex,int pageSize,string whereStr)
        {
            using (DataTable dthn = Bll.News.MULTIPLE(strCon, pageSize, pageIndex, whereStr).DT) 
            {
                if (dthn != null && dthn.Rows.Count > 0)
                {
                    this.repHotNews.DataSource = dthn.DefaultView;
                    this.repHotNews.DataBind();
                }
            }
            
        }
        #endregion

        #region Ad
        protected DataTable CreateSiteTable() 
        {
            DataTable dts = new DataTable();
            dts.Columns.Add("ID", typeof(Int32));
            return dts;
        }

        protected void BindOther() 
        {
            DataTable dts = CreateSiteTable();
            for (int i = 0; i < siteRowNum; i++)
            {
                DataRow newRow = dts.NewRow();
                newRow["ID"] = i + 1;
                dts.Rows.Add(newRow);
            }
            if (dts != null && dts.Rows.Count > 0) 
            {
                this.repAd.DataSource = dts.DefaultView;
                this.repAd.DataBind();
                dts.Dispose();
            }
        }

        protected void repAd_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater repTemp = (Repeater)e.Item.FindControl("repCnIn");
                DataRowView rowv = (DataRowView)e.Item.DataItem;
                int Id = Convert.ToInt32(rowv["ID"]);
                using (DataTable dtO = OtherList(strCon, 2, Id, 3, out countRow, out countPage))   //参数3代表首页广告类型
                {
                    repTemp.DataSource = dtO.DefaultView;
                    repTemp.DataBind();
                }
            }

        }

        protected DataTable OtherList(string strCon,int pageSize,int pageIndex,int adType,out int countRow,out int countPage) 
        {
            DataTable dtO = Bll.CategoryAd.MULTIPLE(strCon, pageSize, pageIndex, adType, out countRow, out countPage);
            return dtO;
        }

        #endregion

        #region ConfigInfo
        protected void ConfigInfo(string strCon) 
        {
            using(DataTable dtConfig = Bll.ConfigInfo.MULTIPLE(strCon))
            {
                if (dtConfig != null && dtConfig.Rows.Count > 0) 
                {
                    dic["Title"] = dtConfig.Rows[0]["SiteName"].ToString();
                    dic["Keywords"] = dtConfig.Rows[0]["SiteKeywords"].ToString();
                    dic["Discription"] = dtConfig.Rows[0]["SiteDescription"].ToString();
                }
            }
        }
        #endregion
    }
}
